package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页",notes = "角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleVo>> findRoleVoPage(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        PageResponse<RoleVo> page = roleService.page(roleDto, pageNum, pageSize);
        return ResponseResult.success(page);
    }

    @PutMapping
    @ApiOperation(value = "角色添加",notes = "角色添加")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<RoleVo> createRole(@RequestBody RoleDto roleDto) {
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>> findCheckedResources(@PathVariable("roleId") Long roleId){
        log.info("根据角色ID查询选中的资源数据:{}", roleId);
        List<String> resourceNos = roleService.findCheckedResources(roleId);
        return ResponseResult.success(resourceNos);
    }

    @PatchMapping
    @ApiOperation(value = "角色修改",notes = "角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState","roleDto.dataScope","roleDto.checkedResourceNos","roleDto.checkedDeptNos","roleDto.id"})
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto) {
        log.info("角色修改:{}", roleDto);
        Boolean updateFlag = roleService.updateRole(roleDto);
        return ResponseResult.success(updateFlag);
    }

    @DeleteMapping("/{roleId}")
    @ApiOperation(value = "角色删除",notes = "角色删除")
    public ResponseResult deleteRole(@PathVariable Long roleId) {
        log.info("角色删除:{}", roleId);
        roleService.deleteRole(roleId);
        return ResponseResult.success();
    }

    @PostMapping("/init-roles")
    @ApiOperation(value = "角色列表查询",notes = "角色列表查询")
    public ResponseResult<List<RoleVo>> getAll() {
        log.info("角色列表查询:");
        List<RoleVo> list = roleService.getAll();
        return ResponseResult.success(list);
    }
}
